﻿using System;

namespace InfoService.Utils
{
    public enum LogLevel
    {
        Info = 1,
        Warning = 2,
        Error = 3,
        Debug = 4,
    }

    public enum InfoServiceModul
    {
        InfoService,
        Weather,
        Feed,
        Twitter,
        RecentlyAdded,
        SkinSettings,
    }


    /// <summary>
    /// Summary description for Logger.
    /// </summary>
    public class Logger
    {
        private Logger()
        {
        }

        private static volatile Logger _Logger = null;

        public static Logger GetInstance()
        {
            // DoubleLock
            if (_Logger == null)
            {
                lock (m_lock)
                {
                    if (_Logger == null) _Logger = new Logger();
                }
            }
            return _Logger;
        }


        // Hilfsfeld für eine sichere Threadsynchronisierung
        private static object m_lock = new object();



        private static System.IO.StreamWriter _Output = null;
        private static string _logFile;


        public static bool LogDebug { get; set; }
        public static bool LogError { get; set; }
        public static bool LogWarning { get; set; }

        public static string LogFile
        {
            get { return _logFile; }
            set
            {
                if (string.IsNullOrEmpty(_logFile))
                {
                    if (System.IO.File.Exists(value))
                    {
                        string dir = System.IO.Path.GetDirectoryName(value) + "\\";
                        string filename = System.IO.Path.GetFileNameWithoutExtension(value) + ".bak";
                        if (System.IO.File.Exists(dir + filename)) System.IO.File.Delete(dir + filename);
                        System.IO.File.Move(value, dir + filename);
                    }
                    _logFile = value;
                }
            }
        }





        public void WriteLog(string message, LogLevel level, InfoServiceModul modul)
        {
            if ((level == LogLevel.Debug && LogDebug) || (level == LogLevel.Error && LogError) || (level == LogLevel.Warning && LogWarning) || level == LogLevel.Info)
            {
                try
                {
                    if (_Output == null)
                    {
                        _Output = new System.IO.StreamWriter(LogFile, true, System.Text.Encoding.Default);
                    }

                    lock (_Output)
                    {
                        _Output.WriteLine(DateTime.Now + " | " + modul + " | " + level + " | " + message);
                        _Output.Flush();
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
        }

        public void WriteLog(TwitterConnector.TwitterArgs twitterArgs, LogLevel level, InfoServiceModul modul)
        {
            string exMessage = "";
            string stackTrace = "";
            string message = "";
            if (!string.IsNullOrEmpty(twitterArgs.ExMessage)) exMessage = twitterArgs.ExMessage;
            if (!string.IsNullOrEmpty(twitterArgs.StackTrace)) stackTrace = twitterArgs.StackTrace;
            if (!string.IsNullOrEmpty(twitterArgs.Message)) message = twitterArgs.Message;
            string logMessage = message + "\n\t\t\t\t\t\t" + exMessage + "\n\t\t\t\t\t\t" + stackTrace;
            logMessage = logMessage.Trim();
            WriteLog(logMessage, level, modul);
        }

        public void WriteLog(WeatherConnector.WeatherArgs weatherArgs, LogLevel level, InfoServiceModul modul)
        {
            string exMessage = "";
            string stackTrace = "";
            string message = "";
            if (!string.IsNullOrEmpty(weatherArgs.ExMessage)) exMessage = weatherArgs.ExMessage;
            if (!string.IsNullOrEmpty(weatherArgs.StackTrace)) stackTrace = weatherArgs.StackTrace;
            if (!string.IsNullOrEmpty(weatherArgs.Message)) message = weatherArgs.Message;
            string logMessage = message + "\n\t\t\t\t\t\t" + exMessage + "\n\t\t\t\t\t\t" + stackTrace;
            logMessage = logMessage.Trim();
            WriteLog(logMessage, level, modul);
        }

        public void WriteLog(FeedReader.FeedArgs feedArgs, LogLevel level, InfoServiceModul modul)
        {
            string exMessage = "";
            string stackTrace = "";
            string message = "";
            if (!string.IsNullOrEmpty(feedArgs.ExMessage)) exMessage = feedArgs.ExMessage;
            if (!string.IsNullOrEmpty(feedArgs.StackTrace)) stackTrace = feedArgs.StackTrace;
            if (!string.IsNullOrEmpty(feedArgs.Message)) message = feedArgs.Message;
            string logMessage = message + "\n\t\t\t\t\t\t" + exMessage + "\n\t\t\t\t\t\t" + stackTrace;
            logMessage = logMessage.Trim();
            WriteLog(logMessage, level, modul);
        }

        public static void CloseLog()
        {
            try
            {
                if (_Output != null)
                {
                    _Output.Flush();
                    _Output.Close();
                    _Output = null;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
    }
}
